// src/components/countdown/index.js
import $CountDown from '../../utils/countdown.js'
Component({
  /**
   * 组件的属性列表
   */
  externalClasses: ['ex-class'],
  options: {
    multipleSlots: true
  },
  properties: {
    endTime: {
      type: Number,
      //  value: 0,
      observer: 'countDown'
    },
    startText: {
      type: String,
      value: ''
    },
    endText: {
      type: String,
      value: ''
    },
    icon: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    days: 0,
    hours: 0,
    min: 0,
    sec: 0,
    ready: false
  },
  timer: null,
  ready: function() {
    this.countDown()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    countDown() {
      const self = this
      // console.log(this.timer)
      if (this.timer) {
        this.timer.update(self.properties.endTime)
      } else {
        this.timer = new $CountDown({
          date: new Date(self.properties.endTime),
          render(date) {
            const days = this.leadingZeros(date.days, 1)
            const hours = this.leadingZeros(date.hours, 1)
            const min = this.leadingZeros(date.min, 1)
            const sec = this.leadingZeros(date.sec, 1)
            self.setData({
              days: days,
              hours: hours,
              min: min,
              sec: sec,
              ready: true // 数据转换成功后显示
            })
          }
        })
      }
    }
  }
})
